package com.sense.kukoo.common.wiget.dialog

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import com.sense.kukoo.common.R
import com.sense.kukoo.common.media.timer.TimerImpl
import com.sense.kukoo.common.utils.ApplicationContext

class TimerSelectionPopupWindow(
    contentView: View, width:Int, height:Int, focusable:Boolean
): PopupWindow(contentView, width, height, focusable) {

    private val TAG: String = javaClass.simpleName

    private lateinit var root_timer_selection:View
    private lateinit var tv_timer_selection_episode_end:TextView
    private lateinit var tv_timer_selection_one_hour:TextView
    private lateinit var tv_timer_selection_45_min:TextView
    private lateinit var tv_timer_selection_30_min:TextView
    private lateinit var tv_timer_selection_15_min:TextView
    private lateinit var tv_timer_selection_off:TextView

    private var mListener: (type:Int)->Unit = {}

    companion object{
        //
        fun create(root: ViewGroup):TimerSelectionPopupWindow{
            val inflate: View = LayoutInflater.from(root.context).inflate(R.layout.common_pupup_timer_selection, root ,false)
            val width = ApplicationContext.dp2px(254f)
            val height =  ApplicationContext.dp2px(266.5f)
            val popupWindow = TimerSelectionPopupWindow(inflate, width, height, true)
            //
            popupWindow.isTouchable = true
            popupWindow.isOutsideTouchable = true
            popupWindow.elevation = 4f
            //
            popupWindow.initView()
            return popupWindow
        }
    }

    fun initView(){
        root_timer_selection = contentView.findViewById<View>(R.id.root_timer_selection)
        tv_timer_selection_episode_end = contentView.findViewById<TextView>(R.id.tv_timer_selection_episode_end)
        tv_timer_selection_one_hour = contentView.findViewById<TextView>(R.id.tv_timer_selection_one_hour)
        tv_timer_selection_45_min = contentView.findViewById<TextView>(R.id.tv_timer_selection_45_min)
        tv_timer_selection_30_min = contentView.findViewById<TextView>(R.id.tv_timer_selection_30_min)
        tv_timer_selection_15_min = contentView.findViewById<TextView>(R.id.tv_timer_selection_15_min)
        tv_timer_selection_off = contentView.findViewById<TextView>(R.id.tv_timer_selection_off)
        //
        tv_timer_selection_episode_end.setOnClickListener(mOnClickListener)
        tv_timer_selection_one_hour.setOnClickListener(mOnClickListener)
        tv_timer_selection_45_min.setOnClickListener(mOnClickListener)
        tv_timer_selection_30_min.setOnClickListener(mOnClickListener)
        tv_timer_selection_15_min.setOnClickListener(mOnClickListener)
        tv_timer_selection_off.setOnClickListener(mOnClickListener)
    }

    private val mOnClickListener:View.OnClickListener = object :View.OnClickListener{
        override fun onClick(v: View) {
            val id = v.id
            when (id) {
                R.id.tv_timer_selection_episode_end -> { mListener(TimerImpl.TYPE_WHEN_CURRENT_EPISODE_END) }
                R.id.tv_timer_selection_one_hour -> { mListener(TimerImpl.TYPE_60_MIN) }
                R.id.tv_timer_selection_45_min -> { mListener(TimerImpl.TYPE_45_MIN) }
                R.id.tv_timer_selection_30_min -> { mListener(TimerImpl.TYPE_30_MIN) }
                R.id.tv_timer_selection_15_min -> { mListener(TimerImpl.TYPE_15_MIN) }
                R.id.tv_timer_selection_off -> { mListener(TimerImpl.TYPE_OFF) }
                else -> {}
            }
            dismiss()
        }
    }

    fun setListener(l:(type:Int)->Unit):TimerSelectionPopupWindow{
        mListener = l
        return this
    }

    fun show(anchor:View):TimerSelectionPopupWindow{
        val xOff = - ApplicationContext.dp2px(100f)//todo 距离计算
        val yoff = -(ApplicationContext.dp2px(270.5f) + anchor.height)
        showAsDropDown(anchor, xOff, yoff)
        //
        return this
    }
}